﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	public class UserAuthController : DefaultController
	{
		public UserAuthController(IAuthenticationService authenticationService)
		{
			this.AuthenticationService = authenticationService;
		}

		public IAuthenticationService AuthenticationService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Login(string returnUrl)
		{
			var value = new UserAuthentication
			{
				ReturnUrl = returnUrl
			};

			return View(Views.Login, value);
		}

		[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
		public ActionResult Login(UserAuthentication value)
		{
			try
			{
				AuthenticationService.Login(value);

				Application.SessionDestroy();

				var url = value.ReturnUrl;

				if (!url.HasValue())
				{
					return RedirectToRoute(Routes.UserDashboardIndex);
				}

				return Redirect(url);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Login, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Logout()
		{
			AuthenticationService.Logout();

			Application.SessionDestroy();

			return Redirect(AuthenticationService.ReturnUrl);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Reset()
		{
			return View(Views.Reset, new UserAuthenticationReset());
		}

		[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
		public ActionResult Reset(UserAuthenticationReset value)
		{
			try
			{
				AuthenticationService.Reset(value);

				return Status(Resources.Messages.UserPasswordReset);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Reset, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Update()
		{
			var value = new UserAuthenticationUpdate
			{
				EmailAddress = Identity.Name
			};

			return View(Views.Update, value);
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize, ValidateAntiForgeryToken]
		public ActionResult Update(UserAuthenticationUpdate value)
		{
			try
			{
				AuthenticationService.Update(value);

				Application.SessionDestroy();

				return RedirectToRoute(Routes.UserLogin);
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, value);
		}
	}
}